package com.jketing.controller.ams.sys;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.jketing.controller.BaseController;
import com.jketing.entity.ams.sys.Function;
import com.jketing.entity.ams.sys.Role;
import com.jketing.service.ams.sys.OperationService;
import com.jketing.service.ams.sys.RoleService;

/**
 * Controller 请求处理
 * 
 * @author  李荣华
 * @version v1.0
 * @date    2017-12-22
 * @since   Spring-mvc 4.3.8
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController<Role,String>{
	/**
	 *  service 	服务层自动注入
	 */
	private RoleService service;
	/**
	 *  service 自动注入服务层操作实例
	 */
	@Autowired
	public void setService(RoleService service) {
		super.setBaseService(service);
		this.service = service;
	}
	/**
	 *  bs 	服务层自动注入
	 */
	@Autowired
	private OperationService bs;
	
	@Override
	protected String getPrefixPath() {
		return "sys/role/";
	}

	@Override
	protected String getQueryMode() {
		return "Entity";
	}
	
	@Override
	protected Role getQuery(Role query) {
		String type = (String)getSession().getAttribute("role_type");
		if("redirect".equals(type)) {
			if("Entity".equals(getQueryMode())) {
				query = (Role)getSession().getAttribute("role_query");
			}
		}
		//可在此处增加修改查询条件的代码
		return query;
	}
	
	/**
	 *  初始化新增编辑页
	 */
	@Override
	protected void initAdd(Map<String, Object> map) {
		map.put("butList", bs.findAll());
	}
	
	/**
	 *  初始化更新编辑页
	 */
	@Override
	protected void initEdit(Map<String, Object> map) {
		Role entity = (Role)map.get("entity");
		List<Function> funList = service.getFunList(entity.getId());
		map.put("butList", bs.findAll());
		map.put("funList", funList);
		map.put("resultJson", service.getFunButJson(entity.getId(), funList));
	}
	
	/**
	 *  保存前执行,用于保存之前再次设置要保存的实体属性或执行其它保存逻辑
	 */
	@Override
	protected Role beforeSave(Role entity,MultipartFile file) {
		String resultJson = (String)request.getParameter("resultJson");
		setExecuteSave(false);//不执行基类的保存
		return service.save(entity,resultJson);
	}

	/**
	 *  更新前执行,用于更新之前再次设置要更新的实体属性或执行其它更新逻辑
	 */
	@Override
	protected Role beforeUpdate(Role entity,MultipartFile file) {
		String resultJson = (String)request.getParameter("resultJson");
		setExecuteSave(false);//不执行基类的保存
		return service.update(entity,resultJson);
	}
}
